package lanou3g.myfirstapp36kr.activity;

import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore.Images.Media;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.FileOutputStream;

import cn.bmob.v3.BmobUser;
import lanou3g.myfirstapp36kr.MyUser;
import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.base.BaseActivity;
import lanou3g.myfirstapp36kr.selectplaces.PlacesActivity;

/**
 * Created by dllo on 16/8/19.
 */
public class AccountMessageActivity extends BaseActivity {
    public static final String ACTION_CHANGE = "lanou3g.myfirstapp36kr.activity.ACTION_CHANGE";
    private Intent broadcast;
    private ImageView imageViewPic;
    private Bitmap bitmap;
    private TextView textViewPlace;
    private MyUser myUser;
    private String place;
    private TextView textViewNick;
    private FrameLayout frameLayoutPickPic;
    private TextView textViewPickPicCancel;
    private TextView textViewPickFromPics;

    @Override
    protected int getLayout() {
        return R.layout.activity_acount_message;
    }

    @Override
    protected void initView() {
        textViewPickPicCancel = (TextView) findViewById(R.id.tv_pick_pic_cancel);
        textViewPickFromPics = (TextView) findViewById(R.id.tv_pick_from_pics);
        frameLayoutPickPic = (FrameLayout) findViewById(R.id.pop_frame_layout_pick_pic);
        myUser = BmobUser.getCurrentUser(MyUser.class);

        textViewPlace = (TextView) findViewById(R.id.tv_account_place);
        textViewNick = (TextView) findViewById(R.id.tv_message_nick);
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        imageViewPic = (ImageView) findViewById(R.id.iv_qq_account);
        //int id = sharedPreferences.getInt("id",R.drawable.myqq);
        String path = sharedPreferences.getString("path","");
        if(path == ""){
            imageViewPic.setImageResource(R.mipmap.ic_launcher);
        }else {
        bitmap = BitmapFactory.decodeFile(path);
        imageViewPic.setImageBitmap(bitmap);

        }
        findViewById(R.id.relative_layout_places).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AccountMessageActivity.this, PlacesActivity.class);
                startActivityForResult(intent,102);
            }
        });
        findViewById(R.id.relative_nick).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AccountMessageActivity.this,NickActivity.class);
                startActivityForResult(intent,103);
            }
        });

        findViewById(R.id.iv_back_my_account_message).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        broadcast = new Intent();
        broadcast.setAction(ACTION_CHANGE);


    }

    @Override
    protected void initData() {
        imageViewPic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                frameLayoutPickPic.setVisibility(View.VISIBLE);
            }
        });
        textViewPickPicCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                frameLayoutPickPic.setVisibility(View.GONE);
            }
        });
        textViewPickFromPics.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                //动作  选择
                intent.setAction(intent.ACTION_PICK);
                //通过URI 指定跳转到系统图库
                intent.setData(Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent,101);
            }
        });
        //拉取网络数据地址
        if (myUser != null){
            String placeNet = myUser.getPlaces();
            if (placeNet != null){
                textViewPlace.setText(placeNet + "");
            }else {
                textViewPlace.setText("未选择所在地");
            }
        }
        if (myUser != null){
            String nickNet = myUser.getNick();
            if (nickNet != null){
                textViewNick.setText(nickNet + "");
            }else {
                textViewNick.setText("未显示");
            }
        }





    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == RESULT_OK){
            //重新获取的URI指向单独选择的那张图片
            Uri uri = data.getData();
            //通过ContentResolver进行查询
            ContentResolver cr = this.getContentResolver();
            Cursor cursor = cr.query(uri,null,null,null,null);
            //查询之后Curson对象中只有一条数据
            if (cursor != null && cursor.moveToFirst()){
                //获取到该图片存贮的路径
                String path = cursor.getString(cursor.getColumnIndex(
                        Media.DATA
                ));
                bitmap = BitmapFactory.decodeFile(path);
                imageViewPic.setImageBitmap(bitmap);
                SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("path",path);
                editor.apply();
                sendBroadcast(broadcast);

            }

        }
        if (requestCode == 102 && resultCode == 98){
            //设置地址
            place = data.getStringExtra("selectedPlaces");
            textViewPlace.setText(place);


        }
        if (requestCode == 103 && resultCode == 97){
            String nick = data.getStringExtra("stringWrite");
            textViewNick.setText(nick);


        }

    }

}
